package com.vnaloha.common.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.vnaloha.common.model.Document;
import com.vnaloha.util.CustomHibernateDaoSupport;

@Repository
public class DocumentDAOImpl extends CustomHibernateDaoSupport implements DocumentDAO{
	
	private Logger logger = LoggerFactory.getLogger(DocumentDAOImpl.class);
	
	/*
	 * (non-Javadoc)
	 * @see com.vnaloha.common.dao.DocumentDAO#save(com.vnaloha.common.model.Document)
	 */
	@Override
	public void save(Document document) {
		try {
			getHibernateTemplate().save(document);
		} catch (Exception e) {
			logger.error("Error when save document " + e, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.vnaloha.common.dao.DocumentDAO#list()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Document> list() {
		List<Document> list = null;
		try {
			list = getHibernateTemplate().find(" from Document");
		} catch (HibernateException e) {
			logger.error("Error when get list document " + e, e);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * @see com.vnaloha.common.dao.DocumentDAO#get(java.lang.Integer)
	 */
	@Override
	public Document get(Integer id) {

		Document document = getHibernateTemplate().get(Document.class, id);

		if (null != document) {
			return document;
		}
		logger.error("Error when get document by id " + id);
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.vnaloha.common.dao.DocumentDAO#delete(java.lang.Integer)
	 */
	@Override
	public void delete(Integer id) {

		Document document = getHibernateTemplate().get(Document.class, id);

		if (null != document) {
			getHibernateTemplate().delete(document);
		}

	}

}
